#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    string convert(string s, int numRows) {
        string str;
        int n = s.size();
        if (numRows == 1)
            return s;
        int d = 2 * numRows - 2;
        for (int i = 0; i < n; i += d) {
            str += s[i];
        }
        for (int k = 1; k < numRows - 1; k++)
            for (int i = k, j = d - i; i < n || i < n; i += d, j += d) {
                if (i < n)
                    str += s[i];
                if (j < n)
                    str += s[j];
            }
        for (int i = numRows - 1; i < n; i += d) {
            str += s[i];
        }
        return str;
    }
};